package com.ssm.controller;

/*import javax.enterprise.inject.Model;
import javax.servlet.http.HttpServletRequest;*/


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.pojo.*;
import com.ssm.service.AppCategoryServiceDao;
import com.ssm.service.AppVersionServiceDao;
import com.ssm.service.DataDictionaryServiceDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ssm.repository.BackendUserMapper;
import com.ssm.service.BackendUserServiceDao;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping(value="/manage/")
public class BackendUserController {
    @Autowired
	private BackendUserServiceDao BackendUserServiceDao;
	@Autowired
	private com.ssm.service.DataDictionaryServiceDao DataDictionaryServiceDao;
    @Autowired
    private com.ssm.service.AppCategoryServiceDao AppCategoryServiceDao;
    @Autowired
    private com.ssm.service.AppInfoServiceDao AppInfoServiceDao;
    @Autowired
    private com.ssm.service.AppVersionServiceDao AppVersionServiceDao;
	/*
	后台登陆页面
	 */
	@RequestMapping(value="/backendlogin")
	public String login() {
		return "backendlogin";
	}

	/*
      app审核信息提示页面
    */
	@RequestMapping(value="/backend/app/list")
	public String backendList(@RequestParam(required=true,defaultValue="1") Integer page,HttpServletRequest request, Model model,HttpSession session) {
		List<DataDictionary> SDictionaries = DataDictionaryServiceDao.selectBySTName();//所属平台
		model.addAttribute("flatFormList",SDictionaries);
        List<AppCategory> OneCategories = AppCategoryServiceDao.selectByPId(null);//一级分类
        model.addAttribute("categoryLevel1List",OneCategories);


        PageHelper.startPage(page,2);
        List<com.ssm.pojo.AppInfo> appInfo=null;
        if(request.getParameter("querySoftwareName")!=""&& request.getParameter("querySoftwareName")!=null
                ||session.getAttribute("querySoftwareName")!=null
                ||request.getParameter("queryFlatformId")!=""&& request.getParameter("queryFlatformId")!=null
                ||session.getAttribute("queryFlatformId")!=null
                ||request.getParameter("queryCategoryLevel1")!=""&& request.getParameter("queryCategoryLevel1")!=null
                ||session.getAttribute("queryCategoryLevel1")!=null
                ||request.getParameter("queryCategoryLevel2")!=""&& request.getParameter("queryCategoryLevel2")!=null
                ||session.getAttribute("queryCategoryLevel2")!=null
                ||request.getParameter("queryCategoryLevel3")!=""&& request.getParameter("queryCategoryLevel3")!=null
                ||session.getAttribute("queryCategoryLevel3")!=null){


            String querySoftwareName=request.getParameter("querySoftwareName");
            if (querySoftwareName==null){
                querySoftwareName=null;
                if(session.getAttribute("querySoftwareName")!=null){
                    querySoftwareName=(String)session.getAttribute("querySoftwareName");
                }
            }else if(querySoftwareName==""){
                querySoftwareName=null;
                session.removeAttribute("querySoftwareName");
            }else{
                session.setAttribute("querySoftwareName",querySoftwareName);
            }

            String queryFlatformId=request.getParameter("queryFlatformId");
            if(queryFlatformId==""){
                queryFlatformId="0";
                session.removeAttribute("queryFlatformId");
            }else if(queryFlatformId==null){
                queryFlatformId="0";
                if(session.getAttribute("queryFlatformId")!=null){
                    queryFlatformId=(String)session.getAttribute("queryFlatformId");
                }
            }else{
                session.setAttribute("queryFlatformId",queryFlatformId);
            }

            String queryCategoryLevel1=request.getParameter("queryCategoryLevel1");
            if(queryCategoryLevel1==""){
                queryCategoryLevel1="0";
                session.removeAttribute("queryCategoryLevel1");
            }else if(queryCategoryLevel1==null){
                queryCategoryLevel1="0";
                if(session.getAttribute("queryCategoryLevel1")!=null){
                    queryCategoryLevel1=(String)session.getAttribute("queryCategoryLevel1");
                }
            }else{
                session.setAttribute("queryCategoryLevel1",queryCategoryLevel1);
            }

            String queryCategoryLevel2=request.getParameter("queryCategoryLevel2");
            if(queryCategoryLevel2==""){
                queryCategoryLevel2="0";
                session.removeAttribute("queryCategoryLevel2");
            }else if(queryCategoryLevel2==null){
                queryCategoryLevel2="0";
                if(session.getAttribute("queryCategoryLevel2")!=null){
                    queryCategoryLevel2=(String)session.getAttribute("queryCategoryLevel2");
                }
            }else{
                session.setAttribute("queryCategoryLevel2",queryCategoryLevel2);
            }

            String queryCategoryLevel3=request.getParameter("queryCategoryLevel3");
            if(queryCategoryLevel3==""){
                queryCategoryLevel3="0";
                session.removeAttribute("queryCategoryLevel3");
            }else if(queryCategoryLevel3==null){
                queryCategoryLevel3="0";
                if(session.getAttribute("queryCategoryLevel3")!=null){
                    queryCategoryLevel3=(String)session.getAttribute("queryCategoryLevel3");
                }
            }else{
                session.setAttribute("queryCategoryLevel3",queryCategoryLevel3);
            }

            appInfo = AppInfoServiceDao.selectBySHType(querySoftwareName, Integer.parseInt(queryFlatformId), Integer.parseInt(queryCategoryLevel1), Integer.parseInt(queryCategoryLevel2), Integer.parseInt(queryCategoryLevel3));
        }else {
            appInfo= AppInfoServiceDao.selectBySHType(null, 0, 0, 0, 0);
        }

        PageInfo<AppInfo> pages=new PageInfo<AppInfo>(appInfo);
        model.addAttribute("appInfoList",appInfo);
        model.addAttribute("pages",pages);
		return "backend/applist";
	}

    /*
     二级分类根据一级分类联动
      */
    @ResponseBody
    @RequestMapping(value="/backend/app/categorylevellist.json")
    public  Object twoChang(HttpServletRequest request){
        String parentId=request.getParameter("pid");
        List<AppCategory> TwoCategories = AppCategoryServiceDao.selectByTTPId(parentId);

        return JSONArray.toJSONString(TwoCategories);
    }

	/****************************************************************************************************/

	/*
	登陆后台主页面
	 */
	@RequestMapping(value="/dologin")
	public String main(HttpServletRequest request, HttpSession session){
		
		String  userCode = request.getParameter("userCode");
		String  userPassword= request.getParameter("userPassword");
		BackendUser userSession =BackendUserServiceDao.loginCheck(userCode, userPassword);
		if(userSession==null){
			return "backendlogin";
		}
		session.setAttribute("userSession",userSession);
		return "backend/main";
	}

	/*
	后台注销
	 */
	@RequestMapping(value="/logout")
	public String logout(HttpSession session) {
		session.removeAttribute("userSession");
		return "backendlogin";
	}

	/**********************************************************************************************************/
    /*
    审核app信息页面
     */
    @RequestMapping(value="/backend/app/check")
    public String check(HttpServletRequest request, Model model,HttpSession session) {
        String aid= request.getParameter("aid");
        String vid= request.getParameter("vid");
        AppInfo appInfo = AppInfoServiceDao.selectOneById(Integer.parseInt(aid));
        AppVersion appVersion = AppVersionServiceDao.selectAVById(Integer.parseInt(vid));
        model.addAttribute("appInfo",appInfo);
        model.addAttribute("appVersion",appVersion);
        return "backend/appcheck";
    }

    /*
    审核通过或不通过
     */
    @RequestMapping(value="/backend/app/checksave")
    public String checksave(HttpServletRequest request,HttpSession session) {
        String status= request.getParameter("status");
        String id=request.getParameter("id");
        int i=0;
        if (status=="2"){
            i= AppInfoServiceDao.updateBySId(Integer.parseInt(status), Integer.parseInt(id));
        }else{
            i = AppInfoServiceDao.updateBySId(Integer.parseInt(status), Integer.parseInt(id));
        }

        return "redirect:/manage/backend/app/list";
    }
}
